package middol.utils

import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import org.apache.poi.util.IOUtils
import org.jodconverter.core.DocumentConverter
import org.springframework.beans.factory.annotation.Autowired

import javax.servlet.ServletOutputStream

/**
* @Description:    office文件转PDF
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/11/13 9:43
* @Version:        1.0
*/
@CompileStatic
class OfficeService {

    @Autowired
    DocumentConverter documentConverter

    /**
     * 预览PDF
     * @param inFilePath
     * @param outFilePath
     * @return
     */
    def previewPdf(String inFilePath, String outFilePath, OutputStream outputStream) {
        File inFile = new File(inFilePath)
        File outFile = new File(outFilePath)
        if(!outFile.exists()){
            convertPdf2(inFile, outFile)
        }
        FileInputStream fis = new FileInputStream(outFile)
        IOUtils.copy(fis, outputStream)
        fis.close()
        outputStream.close()
    }

    /**
     * 文件转换PDF
     * @param inFile
     * @param outFile
     * @return
     */
    def convertPdf2(File inFile, File outFile){
        documentConverter.convert(inFile).to(outFile).execute()
    }

}
